
#include "StateCoinsPopUp.h"
#include "ProgressManager.h"
#include "StateManager.h"
#include "InputManager.h"
#include "AudioUtils.h"
#include "ScaleEffect.h"

void StateCoinsPopUp::loadContent()
{
    m_oEnter = new TimeLineEffect(false, false);
    m_oExit = new TimeLineEffect(false, false);
    
    m_oFade = new RenderableEntity("fade", Vector2(0, 0));
    m_oFade->setScale(Vector2(640, 960));
    m_oFade->setColor(COLOR_BLACK);
    m_oFade->setAlpha(0);
    
    m_oBg = new RenderableEntity("CoinsPopup_BG", Vector2(320, 480));
    m_oBg->setCentered();
    m_oBg->setLiving(true);
    
    m_oRibbon = new RenderableEntity("MENU_0009_ribbon", Vector2(320, 760));
    m_oRibbon->setCentered();
    m_oRibbon->setScale(Vector2(0.8f, 0.8f));
    m_oRibbon->setLiving(true);
    
    m_oText = new TextEntity("Buy coins", FONT_NAME, 48, Vector2(320, 770), 1, 1, true);
    m_oText->setShadow(true, COLOR_PINK_DARK);
    m_oText->setColor(COLOR_PINK_TEXT);
    m_oText->setCentered();
    
    m_oCloseBg = new RenderableEntity("buttonyesNO", Vector2(520, 220));
    m_oCloseBg->setScale(Vector2(1.6f, 1.3f));
    m_oCloseBg->setCentered();
    m_oCloseBg->changeColorWhenClicked(true);

    m_oClose = new TextEntity("CLOSE", FONT_NAME, 34, Vector2(520, 220), 1, 1);
    m_oClose->setCentered();
    m_oClose->setShadow();
    
    for(int i=0; i<NUM_BUY_COINS_BUTTONS; ++i)
    {
        Vector2 vPos = Vector2(320, 650 - 80 * i);
        
        m_oButtonBG[i] = new RenderableEntity("CoinsPopup_Item", vPos);
        m_oButtonBG[i]->setCentered();
        m_oButtonBG[i]->changeColorWhenClicked(true, Vector4(1.0f, 1.0f, 1.0f, 0.3f));
        m_oButtonBG[i]->setupButton(Vector2(0.60f, 0.60f));

        m_oButtonText[i] = new TextEntity(asBuyCoins[i], FONT_NAME, 32, vPos - Vector2(20, 0), 1, 1, true);
        m_oButtonText[i]->setCentered();
        m_oButtonText[i]->setShadow();
        
        m_oButtonCoin[i] = new RenderableEntity("icon_babycoin", vPos + Vector2(m_oButtonText[i]->getTextSize().x * 0.5f + 10, 0));
        m_oButtonCoin[i]->setCentered();
        m_oButtonCoin[i]->setScale(Vector2(0.4f, 0.4f));
        
        
        m_oEnter->addEffect(new ScaleEffect(m_oButtonBG[i], 0.10, false, false, Vector2(0, 0), Vector2(1.2, 1.2)), 0);
        m_oEnter->addEffect(new ScaleEffect(m_oButtonText[i], 0.10, false, false, Vector2(0, 0), Vector2(1.2, 1.2)), 0);
        m_oEnter->addEffect(new ScaleEffect(m_oButtonCoin[i], 0.10, false, false, Vector2(0, 0), Vector2(0.5, 0.5)), 0);
        
        m_oEnter->addEffect(new ScaleEffect(m_oButtonBG[i], 0.05, false, false, Vector2(1.2, 1.2), Vector2(1, 1)), 0.10);
        m_oEnter->addEffect(new ScaleEffect(m_oButtonText[i], 0.05, false, false, Vector2(1.2, 1.2), Vector2(1, 1)), 0.10);
        m_oEnter->addEffect(new ScaleEffect(m_oButtonCoin[i], 0.05, false, false, Vector2(0.5, 0.5), Vector2(0.4, 0.4)), 0.10);
        
        
        m_oExit->addEffect(new ScaleEffect(m_oButtonBG[i], 0.05, false, false, Vector2(1, 1), Vector2(1.2, 1.2)), 0);
        m_oExit->addEffect(new ScaleEffect(m_oButtonText[i], 0.05, false, false, Vector2(1, 1), Vector2(1.2, 1.2)), 0);
        m_oExit->addEffect(new ScaleEffect(m_oButtonCoin[i], 0.05, false, false, Vector2(0.4, 0.4), Vector2(0.5, 0.5)), 0);
        
        m_oExit->addEffect(new ScaleEffect(m_oButtonBG[i], 0.10, false, false, Vector2(1.2, 1.2), Vector2(0, 0)), 0.05);
        m_oExit->addEffect(new ScaleEffect(m_oButtonText[i], 0.10, false, false, Vector2(1.2, 1.2), Vector2(0, 0)), 0.05);
        m_oExit->addEffect(new ScaleEffect(m_oButtonCoin[i], 0.10, false, false, Vector2(0.5, 0.5), Vector2(0, 0)), 0.05);
    }
    
	m_oEnter->addEffect(new FadeEffect(m_oFade, 0.10, false, false, 0.0f, 0.7f), 0);

	m_oEnter->addEffect(new ScaleEffect(m_oBg, 0.10, false, false, Vector2(0, 0), Vector2(1.2, 1.2)), 0);
	m_oEnter->addEffect(new ScaleEffect(m_oRibbon, 0.10, false, false, Vector2(0, 0), Vector2(1.0, 1.0)), 0);
	m_oEnter->addEffect(new ScaleEffect(m_oText, 0.10, false, false, Vector2(0, 0), Vector2(1.2, 1.2)), 0);
	m_oEnter->addEffect(new ScaleEffect(m_oClose, 0.10, false, false, Vector2(0, 0), Vector2(1.2, 1.2)), 0);
	m_oEnter->addEffect(new ScaleEffect(m_oCloseBg, 0.10, false, false, Vector2(0, 0), Vector2(1.9, 1.5)), 0);
	
	m_oEnter->addEffect(new ScaleEffect(m_oBg, 0.05, false, false, Vector2(1.2, 1.2), Vector2(1, 1)), 0.10);
	m_oEnter->addEffect(new ScaleEffect(m_oRibbon, 0.05, false, false, Vector2(1.0, 1.0), Vector2(0.8, 0.8)), 0.10);
	m_oEnter->addEffect(new ScaleEffect(m_oText, 0.05, false, false, Vector2(1.2, 1.2), Vector2(1, 1)), 0.10);
	m_oEnter->addEffect(new ScaleEffect(m_oClose, 0.05, false, false, Vector2(1.2, 1.2), Vector2(1, 1)), 0.10);
	m_oEnter->addEffect(new ScaleEffect(m_oCloseBg, 0.05, false, false, Vector2(1.9, 1.5), Vector2(1.6, 1.3)), 0.10);
    

	m_oExit->addEffect(new FadeEffect(m_oFade, 0.10, false, false, 0.7f, 0.0f), 0);

	m_oExit->addEffect(new ScaleEffect(m_oBg, 0.05, false, false, Vector2(1, 1), Vector2(1.2, 1.2)), 0);
	m_oExit->addEffect(new ScaleEffect(m_oRibbon, 0.05, false, false, Vector2(0.8, 0.8), Vector2(1.0, 1.0)), 0);
	m_oExit->addEffect(new ScaleEffect(m_oText, 0.05, false, false, Vector2(1, 1), Vector2(1.2, 1.2)), 0);
	m_oExit->addEffect(new ScaleEffect(m_oClose, 0.05, false, false, Vector2(1, 1), Vector2(1.2, 1.2)), 0);
	m_oExit->addEffect(new ScaleEffect(m_oCloseBg, 0.05, false, false, Vector2(1.6, 1.3), Vector2(1.9, 1.5)), 0);
    
	m_oExit->addEffect(new ScaleEffect(m_oBg, 0.10, false, false, Vector2(1.2, 1.2), Vector2(0, 0)), 0.05);
	m_oExit->addEffect(new ScaleEffect(m_oRibbon, 0.10, false, false, Vector2(1.0, 1.0), Vector2(0, 0)), 0.05);
	m_oExit->addEffect(new ScaleEffect(m_oText, 0.10, false, false, Vector2(1.2, 1.2), Vector2(0, 0)), 0.05);
	m_oExit->addEffect(new ScaleEffect(m_oClose, 0.10, false, false, Vector2(1.2, 1.2), Vector2(0, 0)), 0.05);
	m_oExit->addEffect(new ScaleEffect(m_oCloseBg, 0.10, false, false, Vector2(1.9, 1.5), Vector2(0, 0)), 0.05);
}

void StateCoinsPopUp::unloadContent()
{
    delete m_oBg;
    delete m_oClose;
    delete m_oText;
}

void StateCoinsPopUp::enter()
{    
    m_eState = POPUP_ENTER;
    m_oEnter->start(true);
}

void StateCoinsPopUp::update(float dt)
{
    switch(m_eState)
    {
        case POPUP_ENTER:
            if(m_oEnter->isFinished())
            {
                m_eState = POPUP_IDLE;
            }
            break;
            
        case POPUP_IDLE:
            m_oClose->update();
            m_oBg->update();
            m_oRibbon->update();
            m_oText->setScale(m_oRibbon->getScale() / 0.8f);
            
            for(int i=0; i<NUM_BUY_COINS_BUTTONS; ++i)
            {
                m_oButtonBG[i]->update();
                
                if(m_oButtonBG[i]->isClicked())
                {
                    /*
                    SaveGame sSaveGame = ProgressManager::instance().getSavedData();
                    sSaveGame.iCoins += aiBuyCoinsValues[i];
                    ProgressManager::instance().setSavedData(sSaveGame);
                     */
                    
                    
                    
                    m_eState = POPUP_EXIT;
                    m_oExit->start(true);
                    break;
                }
            }
            
            if(m_oClose->isClicked())
            {
                m_oExit->start(true);
                m_eState = POPUP_EXIT;
            }
            break;
            
        case POPUP_EXIT:
            if(m_oExit->isFinished())
            {
                StateManager::instance().popState();
            }
            break;
    }
}

void StateCoinsPopUp::render()
{
    m_oFade->render();
    m_oBg->render();
    m_oRibbon->render();
    m_oText->render();

    m_oCloseBg->render();
    m_oClose->render();
    
    for(int i=0; i<NUM_BUY_COINS_BUTTONS; ++i)
    {
        m_oButtonBG[i]->render();
        m_oButtonText[i]->render();
        m_oButtonCoin[i]->render();
    }
}

void StateCoinsPopUp::exit()
{
}

void StateCoinsPopUp::popup()
{
    StateManager::instance().pushState(instance());
}
